package com.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jt.mapper.ItemDescMapper;
import com.jt.mapper.ItemMapper;
import com.jt.pojo.Item;
import com.jt.pojo.ItemDesc;
import com.jt.vo.ItemVO;
import com.jt.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author :Colin
 * @version 创建时间: 2021/8/9 18:56
 */
@Service
public class ItemServiceImpl implements ItemService {
    @Autowired
    private ItemMapper itemMapper;
    @Autowired
    private ItemDescMapper itemDescMapper;



    @Override
    public PageResult getItemList(PageResult pageResult) {
        Integer pageSize = pageResult.getPageSize();
        Integer pageNum = pageResult.getPageNum();
        Page<Item> page = new Page<>(pageNum,pageSize);
        QueryWrapper queryWrapper = new QueryWrapper<>();
        boolean flag = StringUtils.hasLength(pageResult.getQuery());
        queryWrapper.like(flag,"title",pageResult.getQuery());
        page = itemMapper.selectPage(page, queryWrapper);
        List<Item> records = page.getRecords();
        long total = page.getTotal();
        PageResult pageResult1 = pageResult.setTotal(total).setRows(records);
        return pageResult1;

    }

    @Override
    @Transactional
    public void updateItemStatus(Item item) {

        itemMapper.updateById(item);
    }

    @Override
    @Transactional
    public void saveItem(ItemVO itemVO) {
        Item item = itemVO.getItem();
        item.setStatus(true);
        itemMapper.insert(item);

        ItemDesc itemDesc = itemVO.getItemDesc();
        itemDesc.setId(item.getId());//本来是null,但是MP做了优化,自动回显了
        itemDescMapper.insert(itemDesc);
    }

}
